﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using HySoft.Cash.Business;
using HySoft.Cash.Structure;
using System.Configuration;
using System.Web.Profile;
using System.Data.SqlClient;
using System.Data;
using HySoft.Cash.BaseClass;
using HiLand.Utility.Data;
using HySoft.Cash;

namespace PayRoller.Manage.OrgInfo
{
    public class Index : ParentPage, IRequiresSessionState
    {
        // Fields
        public decimal fCreditAvailable;
        public decimal fCreditBalance;
        public decimal fCreditLimit;
        protected HtmlForm form1;
        public string Guid = "";
        public int iActiveCount;
        public int iActiveTerminated;
        public string InvoiceDate = "";
        public string InvoiceSid = "";
        protected Label lbTip;
        public string OrgName = "";
        public string OrgSid = "";
        public string sCreditLimit = "";

        // Methods
        //public double GetClosingBalance(int OrgSid)
        //{
        //    SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", OrgSid) };
        //    DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetClosingBalanceByOrgSid", commandParameters).Tables[0];
        //    if (table.Rows.Count > 0)
        //    {
        //        return Convert.ToDouble(table.Rows[0][0]);
        //    }
        //    return 0.0;
        //}

        public void GetLatestInvoice(int OrgSid)
        {
            SqlParameter[] commandParameters = new SqlParameter[] { new SqlParameter("@OrgSid", OrgSid) };
            DataTable table = SqlHelper.ExecuteDataset(ConfigurationManager.AppSettings["SQLConnString"], CommandType.StoredProcedure, "GetLatestInvoiceByOrg", commandParameters).Tables[0];
            if (table.Rows.Count > 0)
            {
                DataRow firstRow= table.Rows[0];
                DateTime lastInvoiceDate= HiLand.Utility.Data.SafeDateTime.Min;
                DateTime.TryParse(firstRow["InvoiceDate"].ToString(),out lastInvoiceDate);
                this.InvoiceSid = firstRow["Sid"].ToString();
                this.InvoiceDate = lastInvoiceDate.ToString(AppSettings.DateFormatAU,AppSettings.CurrentCulture);
                this.Guid = firstRow["Guid"].ToString();
            }
        }

        public void GetOrgInfo(int OrgSid)
        {
            using (TorginfoBN obn = new TorginfoBN(this.Page))
            {
                TorginfoDT odt = obn.Get(OrgSid);
                this.OrgName = odt.OrgName;
                this.fCreditLimit = Convert.ToDecimal(odt.CreditLimit);
                this.fCreditBalance = Convert.ToDecimal(odt.CreditBalance);
                this.fCreditAvailable = Convert.ToDecimal(odt.AvailableBalance);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.m_ModuleCode = "";
            base.CheckLogin(true);
            base.CheckPerm();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (base.Request["OrgSid"] != null)
            {
                try
                {
                    this.OrgSid = base.Request["OrgSid"].ToString();
                    int orgSid = Convert.ToInt32(this.OrgSid);
                    this.GetOrgInfo(orgSid);
                    this.iActiveCount = this.ReturnCount(orgSid, 1);
                    this.iActiveTerminated = this.ReturnCount(orgSid, 0);
                    this.GetLatestInvoice(orgSid);
                }
                catch (Exception exception)
                {
                    this.lbTip.Text = exception.Message;
                }
            }
        }

        public int ReturnCount(int OrgSid, int IsValid)
        {
            TempinfoBN obn = new TempinfoBN(this.Page);
            DataTable list = new DataTable();
            obn.QueryOrgSid(OrgSid);
            obn.QueryIsValid(IsValid);
            list = obn.GetList();
            obn.Close();
            obn.Dispose();
            return list.Rows.Count;
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }
    }

}
